package cn.school.controller.workflow;


import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.workflow.service.GoodsBuyApplyService;
import cn.school.workflow.vo.GoodsBuyApplyVO;
import cn.school.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goodsBuy")
@Api(tags = "物品申购")
public class GoodsBuyApplyController {

    @Autowired
    private GoodsBuyApplyService goodsBuyApplyService;
    /**
     * 查询公文信息
     * @return
     * @throws ApiException
     */
    @PostMapping("/pageList")
    @ApiOperation("list => 分页查询申购申请单")
    public CommonResult<CommonPage<GoodsBuyApplyVO>> findGoodsBuyApplyPageList(@RequestBody GoodsBuyApplyVO vo) throws ApiException{
        IPage<GoodsBuyApplyVO> pageList =  goodsBuyApplyService.findGoodsBuyApplyPageList(vo);
        return  CommonResult.success(CommonPage.restPage(pageList));
    }

    @PostMapping("/cancel")
    @ApiOperation("cancel => 撤销申购申请")
    public CommonResult<CommonPage<GoodsBuyApplyVO>> cancelDocumentApply(@RequestBody WorkFlowVO vo) throws ApiException{
        boolean flag =  goodsBuyApplyService.cancelGoodsBuyApply(vo);
        if(flag){
            return CommonResult.success();
        }
        return  CommonResult.failed("流程取消失败");
    }

    @PostMapping("/create")
    @ApiOperation("create => 创建申购流程")
    public CommonResult<Integer> createDocumentApply(@RequestBody GoodsBuyApplyVO  vo) throws ApiException{

        int flag =  goodsBuyApplyService.createGoodsBuyApply(vo);
        if(flag>0){
            return CommonResult.success();
        }
        return  CommonResult.failed("流程创建失败");
    }

    @GetMapping("/detail/{processInstanceId}")
    @ApiOperation("detail => 申购详情")
    public CommonResult<GoodsBuyApplyVO> findDocumentApplyDetail(@PathVariable("processInstanceId") Long processInstanceId) throws ApiException{
        GoodsBuyApplyVO goodsBuyApplyVO =  goodsBuyApplyService.findGoodsBuyApplyDetail(processInstanceId);
        if(goodsBuyApplyVO!=null){
            return CommonResult.success(goodsBuyApplyVO);
        }
        return  CommonResult.failed("查询不单申请单");
    }
}
